local PLAYER = FindMetaTable("Player");

--Client info, not to be confused with the actual 'Character' info. This can define information like time spent on server, scores etc.
function PLAYER:ClientSetup()

	self:SetData( "ID", self:FormatID() );
	self:SetData( "Characters", {} );
	self:SetData( "SelectedCharacter", 1 );
	
end
	

hook.Add("PlayerAuthed", "GAME.ClientAuthed", function(ply, steamid, uniqueid)

	ply:SetVar("Nein", true);

end);	

hook.Add("PlayerInitialSpawn", "GAME.ClientInitialSpawn", function(ply)
	
	if ply:FirstJoin() then
	
		ply:ClientSetup();
		ply:CharacterSetup();
	
	else
	
		ply:InitData();
		ply:CharacterInitialize();
		
	end
	
	timer.Simple(.5, function() ply:SetVar("Nein", nil); end);

end);

function PLAYER:SetData(index, info, sync)
	
	local PlayerData = self:GetVar("PlayerData", {}) or {};
	
		PlayerData[index] = info;
	
	self:SetVar("PlayerData", PlayerData);
	
	self:SaveData();
	
	if sync then datastream.StreamToClients( self, "PlayerDataSync", self:GetVar("PlayerData") ); end
	
	return PlayerData;
	
end

function PLAYER:GetData(index)
	
	return self:GetVar("PlayerData")[index];
	
end

--Remove useless steam junk from our player Identity.
function PLAYER:FormatID()

	local SteamID = self:SteamID() or "STEAM_0:0:0";

	local s = string.gsub( SteamID, "STEAM", "" );
	
		s = string.gsub( s, ":", "" );
		s = string.gsub( s, "_", "" );
		s = string.gsub( s, " ", "" );
	
	return s;
	
end

function PLAYER:SaveData()
	
	file.Write( GAME.FolderName.."/Clients/"..self:FormatID()..".txt", glon.encode( self:GetVar("PlayerData") or {} ) );
	
end

function PLAYER:InitData()

	local FilePath = GAME.FolderName.."/Clients/"..self:FormatID()..".txt";
	
	if file.Exists( FilePath ) then
		
		local Decoded = glon.decode( file.Read( FilePath ) );
		
		self:SetVar("PlayerData", Decoded );
		datastream.StreamToClients( self, "PlayerDataSync", Decoded );
		
	end
	
end

function PLAYER:FirstJoin()

	if !file.Exists( GAME.FolderName.."/Clients/"..self:FormatID()..".txt" ) then
	
		return true;
		
	end
	
	return false;

end

/*////////////////////////////////////////
			*Server Data*
*/////////////////////////////////////////

GAME.MasterTab = {};

function GAME.SetData( index, info ) 

	GAME.MasterTab[index] = info;
	GAME.SaveData();

end

function GAME.GetData( index )

	return GAME.MasterTab[index] or {};

end

function GAME.InitData()

	local FilePath = GAME.FolderName.."/Server.txt";
	
	if file.Exists( FilePath ) then
		
		local Decoded = glon.decode( file.Read( FilePath ) );
		
		for index, info in pairs(Decoded) do

			GAME.MasterTab[index] = info;
			
		end
		
	end

end

function GAME.SaveData()

	file.Write( GAME.FolderName.."/Server.txt", glon.encode( GAME.MasterTab or {} ) );

end

GAME.InitData()

//Create map based objectives which are set in debug. Then create map based quests which consist of positions and kind of work like tiramasus stash system